﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection.Emit;

namespace Kokomo.PeInspector.Reflection.Disassembly
{
	/// <summary>
	/// Represents a branch within MSIL.
	/// </summary>
	public class MsilBranch : MsilTransition
	{
		/// <summary>
		/// Gets the target of the branch,
		/// as an offset from the beginning of the procedure.
		/// </summary>
		public int TargetOffset { get; private set; }

		/// <summary>
		/// Intitializes a new instance of <see cref="MsilBranch"/>.
		/// </summary>
		/// <param name="sourceOffset">The instruction </param>
		/// <param name="type"></param>
		/// <param name="targetOffset"></param>
		public MsilBranch(
			int sourceOffset,
			bool isConditional,
			int targetOffset
			)
			:
			base(sourceOffset, isConditional)
		{
			this.TargetOffset = targetOffset;
		}

		public override string ToString()
		{
			return string.Format("{0:X4}: {1}> {2:X4}",
				this.SourceOffset,
				this.IsConditional ? "?" : "=",
				this.TargetOffset
				);
		}
	}
}
